package com.wbj.sensor.service

import android.annotation.SuppressLint
import android.app.Service
import android.content.Intent
import android.content.res.Resources
import android.graphics.PixelFormat
import android.os.IBinder
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.wbj.sensor.R

class OverlayService : Service() {

    override fun onCreate() {
        super.onCreate()
        initMenuWindow()
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    /**
     * 初始化 MenuWindow 的 View, 并添加到 WindowManager 中
     */
    @SuppressLint("WrongConstant")
    private fun initMenuWindow() {
        val layoutParams = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT)
        layoutParams.type = WindowManager.LayoutParams.LAST_SUB_WINDOW
        layoutParams.format = PixelFormat.TRANSLUCENT
        layoutParams.gravity = Gravity.LEFT or Gravity.TOP
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH or
                WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
//        layoutParams.privateFlags =
//            layoutParams.privateFlags or WindowManager.LayoutParams.PRIVATE_FLAG_IS_SCREEN_DECOR
        layoutParams.x = 0
        layoutParams.y = 0
        val inflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val view: View =
            inflater.inflate(R.layout.window_menu, null, false)
        view.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        windowManager.addView(view, layoutParams)
    }
}